import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Classify } from "src/entities/classify.entity";
import { User } from "src/entities/user.entity";
import { ArticleInput, ClassifyInput, ClassifySortInput, ClassifyWhereInput, PageInput } from "src/graphql";
import { ArticleService } from "src/services/article/article.service";
import { ClassifyService } from "src/services/classify/classify.service";
import { UserService } from "src/services/user/user.service";

@Resolver('Classify')
export class ClassifyResolver {
  constructor(
    private readonly classifyService: ClassifyService,
    private readonly userService: UserService,
    private readonly articleService: ArticleService,
  ) {}

  @Query('classify')
  async getClassifyById(@Args() id: number) {
    return await this.classifyService.findOne(id);
  }

  @Query('classifys')
  async getClassifys(@Args('page') page: PageInput,@Args('sort') sort:ClassifySortInput,@Args('where') where:ClassifyWhereInput) {
    return await this.classifyService.getPagination(page,sort,where);
  }

  @Mutation()
  async addClassify(@Args('classify') classify:ClassifyInput) {
    return await this.classifyService.addOne(<Classify>classify);
  }

  @Mutation()
  async removeClassify(@Args() id: number) {
    return await this.classifyService.removeOne(id);
  }

  @Mutation()
  async modifyClassify(@Args('id') id: number, @Args('classify') classify:ClassifyInput) {
    return await this.classifyService.modifyOne(id,<Classify>classify);
  }

  @ResolveProperty(() => User,{
    name:'_user'
  })
  async user(@Parent() classify){
    const { user_id } = classify;
    return await this.userService.findOne(user_id);
  }

  @ResolveProperty(() => User,{
    name:'_articles'
  })
  async articles(@Parent() classify){
    const { id } = classify;
    return await this.articleService.findAll({classify_id:id});
  }
}